package net.javamine.util;

/**
 * 分页工具类
 * @author <a href="mailto: java_lht@163.com">Lht</a>
 * @since 2012-05-24 1.create
 */
public class PaginationUtils {
	
	/**
	 * 通过当前页码和skip跳跃的游标数(limit beginLimit, skip) 得到beginLimit开始的游标
	 * @param pageNum
	 * @param skip
	 * @return
	 */
	public static int getBeginLimit(int pageNum, int skip) {
		return pageNum * skip - skip;
	}
	
	/**
	 * 通过当前记录总数和跳跃的游标数得到总页数
	 * @param recordCount
	 * @param skip
	 * @return
	 */
	public static int getPageCount(int recordCount, int skip) {
		if (recordCount%skip==0) {
			return recordCount/skip;
		} else {
			return recordCount/skip + 1;
		}
	}
	
	/**
	 * 生成分页信息 包括第一页，上一页，下一页，最后一页等
	 * 
	 * @param pageNum
	 *            当前页数
	 * @param pageCount
	 *            总页数
	 * @param recordCount
	 *            总记录数
	 * @param pageUrl
	 *            页面URL
	 * @return
	 */
	public static String getPagination(int pageNum, int pageCount,
			int recordCount, String pageUrl) {

		if(pageCount <= 1){
			return "";
		}
		
		if (pageUrl == null || pageUrl.equals("")) {
			throw new RuntimeException("pageUrl is null or blank String");
		}
		
		String url = pageUrl.contains("?") ? pageUrl : pageUrl + "?";
	
		StringBuffer buffer = new StringBuffer();

		buffer.append("第  " + pageNum + "/" + pageCount + " 页  共  "
				+ recordCount + " 条记录  ");
		buffer.append(pageNum == 1 ? "第一页" : "<a href='" + url
				+ "&pageNum=1'>第一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == 1 ? "上一页" : "<a href='" + url + "&pageNum="
				+ (pageNum - 1) + "'>上一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "下一页" : "<a href='" + url
				+ "&pageNum=" + (pageNum + 1) + "'>下一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "最后一页" : "<a href='" + url
				+ "&pageNum=" + pageCount + "'>最后一页</a>");
		buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
		buffer
				.append("到<input type='text' name='goto_input' style='width:25px;font-size:12px;text-align:center' />页");
		buffer
				.append("<input type='button' name='goto_button' style='width:25px;font-size:12px' value='Go' />");

		buffer.append("<script type='text/javascript'>");
		buffer.append("    function helloweenvsfei_enter(){");

		buffer.append("    	  if(event.keyCode == 13){");
		buffer.append("    		  helloweenvsfei_goto();");
		buffer.append("    		  return false;");
		buffer.append("       }");
		buffer.append("    	  return true;");
		buffer.append("    }");

		buffer.append("function helloweenvsfei_goto(){");
		buffer.append("    var numText = document.getElementsByName('goto_input')[0].value;");
		buffer.append("    var num = parseInt(numText, 10);");
		buffer.append("    if(!num){");
		buffer.append("        alert('页数必须是数字');");
		buffer.append("        return;");
		buffer.append("    }");

		buffer.append("    if(num < 1 || num >" + pageCount + "){");
		buffer.append("        alert('页数必须大于0，且小于总页数" + pageCount + "');");
		buffer.append("        return;");
		buffer.append("    }");
		buffer.append("    location ='" + url + "&pageNum='+num;");
		buffer.append("}");

		buffer.append("document.getElementsByName('goto_input')[0].onkeypress = helloweenvsfei_enter;");
		buffer.append("document.getElementsByName('goto_button')[0].onclick = helloweenvsfei_goto;");
		
		buffer.append("</script>");
		
		return buffer.toString();
	}

} 